package com.woniuxy.service.impl;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.dao.EquipmentChangeDao;
import com.woniuxy.dao.EquipmentDao;
import com.woniuxy.entity.Equipment;
import com.woniuxy.entity.EquipmentChange;
import com.woniuxy.entity.PageBean;
import com.woniuxy.service.EquipmentChangeService;
import com.woniuxy.service.EquipmentService;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EquipmentChangeServiceImpl implements EquipmentChangeService {

    @Autowired
    EquipmentChangeDao equipmentChangeDao;

    //通过输入条件查询设备流动信息，分页展示
    @Override
    public PageBean<EquipmentChange> getEquipmentChangePageByCondition(EquipmentChange equipmentChange, int pageIndex, int pageSize) {
        try {
            PageBean<EquipmentChange> pageBean = new PageBean<>();
            //存储当前页
            pageBean.setPageIndex(pageIndex);
            //存储页大小
            pageBean.setPageSize(pageSize);
            //获取总条数
            int pageCount = equipmentChangeDao.selectEquipmentChangeCount(equipmentChange);
            //存储总条数
            pageBean.setTotalRecord(pageCount);

            pageBean.setBeanList(equipmentChangeDao.selectEquipmentChangeByCondition(equipmentChange, pageIndex, pageSize));
            return pageBean;
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }

    @Override
    public PageBean<EquipmentChange> getEquipmentChangePageByCategory(EquipmentChange equipmentChange, int pageIndex, int pageSize) {
        try {
            PageBean<EquipmentChange> pageBean = new PageBean<>();
            //存储当前页
            pageBean.setPageIndex(pageIndex);
            //存储页大小
            pageBean.setPageSize(pageSize);
            //获取总条数
            int pageCount = equipmentChangeDao.selectEquipmentChangeCountByCategory(equipmentChange);
            //存储总条数
            pageBean.setTotalRecord(pageCount);

            pageBean.setBeanList(equipmentChangeDao.selectEquipmentChangeByCategory(equipmentChange, pageIndex, pageSize));
            return pageBean;
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }
    //添加设备流通记录
    @Override
    public void addEquipmentChange(EquipmentChange equipmentChange) {
        try {
            //调用方法存入
            equipmentChangeDao.appendEquipmentChange(equipmentChange);
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }

    @Override
    public void deleteEquipmentChange(int equipmentChangeId) {
        try {
            //调用方法删除
            equipmentChangeDao.removeEquipmentChange(equipmentChangeId);
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }


}
